home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / components / stylishDataSource.js < prev    next >
Text File  |  2009-10-14  |  3KB  |  89 lines

  1. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  2.  
  3. function StylishDataSource() {}
  4. StylishDataSource.prototype = {
  5.  
  6.     /*
  7.         nsISupports
  8.     */
  9.     QueryInterface: XPCOMUtils.generateQI([Components.interfaces.stylishDataSource, Components.interfaces.nsIClassInfo, Components.interfaces.nsISupports]),
  10.  
  11.  
  12.     /*
  13.         nsIClassInfo
  14.     */
  15.     getInterfaces: function getInterfaces(aCount) {
  16.         var interfaces = [Components.interfaces.stylishDataSource, Components.interfaces.nsIClassInfo, Components.interfaces.nsISupports];
  17.         aCount.value = interfaces.length;
  18.         return interfaces;
  19.     },
  20.     getHelperForLanguage: function getHelperForLanguage(aLanguage) {
  21.         return null;
  22.     },
  23.     classDescription: "Stylish Data Source",
  24.     classID: Components.ID("{d6fe57ea-1126-4dc6-8636-d25d5b901929}"),
  25.     contractID: "@userstyles.org/stylish-data-source;1",
  26.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  27.     flags: 0,
  28.  
  29.  
  30.     /*
  31.         stylishDataSource
  32.     */
  33.     getConnection: function() {
  34.         var storageService = Components.classes["@mozilla.org/storage/service;1"].getService(Components.interfaces.mozIStorageService);
  35.         //xxx what about uris?
  36.         var connection = storageService.openDatabase(this.getFile());
  37.         this.migrate(connection);
  38.         return connection;
  39.     },
  40.  
  41.     getFile: function() {
  42.         if (!this._file) {
  43.             var path = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getCharPref("extensions.stylish.dbFile");
  44.             if (path) {
  45.                 this._file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  46.                 this._file.initWithPath(path);
  47.             } else {
  48.                 this._file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
  49.                 this._file.append("stylish.sqlite");
  50.             }
  51.         }
  52.         return this._file;
  53.     },
  54.  
  55.     /*
  56.         private
  57.     */
  58.     _file: null,
  59.  
  60.     migrate: function(connection) {
  61.         var expectedDataVersion = 2;
  62.         var currentDataVersion = connection.schemaVersion;
  63.         if (currentDataVersion >= expectedDataVersion)
  64.             return;
  65.         connection.beginTransaction();
  66.         switch (currentDataVersion) {
  67.             case 0:
  68.                 connection.executeSimpleSQL("DROP TABLE IF EXISTS styles; CREATE TABLE styles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, code TEXT NOT NULL, enabled INTEGER NOT NULL);");
  69.                 connection.executeSimpleSQL("DROP TABLE IF EXISTS style_meta; CREATE TABLE style_meta (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, style_id INTEGER NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL);");
  70.                 connection.executeSimpleSQL("DROP INDEX IF EXISTS style_meta_style_id; CREATE INDEX style_meta_style_id ON style_meta (style_id);");
  71.             case 1:
  72.                 try {
  73.                     connection.executeSimpleSQL("ALTER TABLE styles ADD COLUMN originalCode TEXT NULL;");
  74.                 } catch (ex) {
  75.                     // this can happen if the user downgrades to a version with schema 1 then upgrades. they will then already have the column.
  76.                 }
  77.         }
  78.         connection.schemaVersion = expectedDataVersion;
  79.         connection.commitTransaction();
  80.     }
  81.  
  82. };
  83.  
  84. var components = [StylishDataSource];
  85. function NSGetModule(compMgr, fileSpec) {
  86.     return XPCOMUtils.generateModule(components);
  87. }
  88.  
  89.